function M9ProductCommon() {
};

M9ProductCommon.instance = function(callbackFunc) {
  var popupBox = new PopupBox(), _self = this;
  
  var model = Model.instance('{{$smarty.const.MODEL_NAME_PRODUCT_COMMON|escape:javascript}}', '{{$smarty.const.MODEL_TITLE_PRODUCT_COMMON|escape:javascript}}', '{{$smarty.const.MODEL_TYPE_PRODUCT|escape:javascript}}');
  
  //Create model here...
  var state1 = State.instance(0, 'Inactived', null);
  var state2 = State.instance(1, 'Customers take the form', null);
  var state3 = State.instance(2, 'Customers return the form', null);
  var state4 = State.instance(3, 'Choose a designer', function(status, now) { return parseInt(2 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state5 = State.instance(4, '(Translate, upload) customer form', function(status, now) { return parseInt(3 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state6 = State.instance(5, 'Download customer form and the photos', function(status, now) { return parseInt(2 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state7 = State.instance(6, '(Complete, upload) the designs', function(status, now) { return parseInt(7 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state8 = State.instance(7, '(Check, send) the designs', function(status, now) { return parseInt(2 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state9 = State.instance(8, 'Customers confirm the designs', null);
  var state10 = State.instance(9, 'Translate the changes', function(status, now) { return parseInt(3 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state11 = State.instance(10, 'Download the changes', function(status, now) { return parseInt(3 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state12 = State.instance(11, 'Upload the original designs', function(status, now) { return parseInt(2 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state13 = State.instance(12, 'Download designs and print out', function(status, now) { return parseInt(2 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state14 = State.instance(13, 'Complete the product', function(status, now) { return parseInt(7 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state15 = State.instance(14, 'Send the product', function(status, now) { return parseInt(7 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state16 = State.instance(15, 'Receive the product', function(status, now) { return parseInt(7 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state17 = State.instance(16, 'Customers take the product', null);
  var state18 = State.instance(17, 'The product has been taken', null);
  var state19 = State.instance(18, 'Fix the problems', function(status, now) { return parseInt(5 - ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400), 10); });
  var state20 = State.instance(19, 'Closed', null);
  var transition1 = Transition.instance('Taken', state2, state3, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition2 = Transition.instance('Remind', state2, state2, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition3 = Transition.instance('Returned', state3, state4, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition4 = Transition.instance('Remind', state3, state3, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition5 = Transition.instance('Choose', state4, state5, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition6 = Transition.instance('Uploaded', state5, state6, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition7 = Transition.instance('Downloaded', state6, state7, '{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}');
  var transition8 = Transition.instance('Uploaded', state7, state8, '{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}');
  var transition9 = Transition.instance('Sent', state8, state9, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition10 = Transition.instance('Approve', state9, state12, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition11 = Transition.instance('Modify', state9, state10, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition12 = Transition.instance('Remind', state9, state9, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition13 = Transition.instance('Translated', state10, state11, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  var transition14 = Transition.instance('Downloaded', state11, state7, '{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}');
  var transition15 = Transition.instance('Uploaded', state12, state13, '{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}');
  var transition16 = Transition.instance('Printed', state13, state14, '{{$smarty.const.USER_TYPE_FACTORY|escape:javascript}}');
  var transition17 = Transition.instance('Completed', state14, state15, '{{$smarty.const.USER_TYPE_FACTORY|escape:javascript}}');
  var transition18 = Transition.instance('Sent', state15, state16, '{{$smarty.const.USER_TYPE_FACTORY|escape:javascript}}');
  var transition19 = Transition.instance('Received', state16, state17, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition20 = Transition.instance('Taken', state17, state18, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition21 = Transition.instance('Remind', state17, state17, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition22 = Transition.instance('Close', state18, state20, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition23 = Transition.instance('Report problems', state18, state19, '{{$smarty.const.USER_TYPE_MANAGER_ASSISTANT|escape:javascript}}');
  var transition24 = Transition.instance('Fixed', state19, state17, '{{$smarty.const.USER_TYPE_CUSTOMER_SERVICE|escape:javascript}}');
  
  transition2.validate = function(status, order, now, registrar) { return (transition2.assignee == registrar.type && ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400) > 14); };
  transition4.validate = function(status, order, now, registrar) { return (transition4.assignee == registrar.type && ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400) > 14); };
  transition12.validate = function(status, order, now, registrar) { return (transition12.assignee == registrar.type && ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400) > 14); };
  transition21.validate = function(status, order, now, registrar) { return (transition17.assignee == registrar.type && ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400) > 14); };
  transition22.validate = function(status, order, now, registrar) { return (transition17.assignee == registrar.type && ((SimpleDate.toStamp(now) - SimpleDate.toStamp(status.date)) / 86400) > 14); };

  transition7.validate = function(status, order, now, registrar) { return (DlmanageUtils.trace(order, status).assignee.userId == registrar.id) };
  transition8.validate = function(status, order, now, registrar) { return (DlmanageUtils.trace(order, status).assignee.userId == registrar.id) };
  transition14.validate = function(status, order, now, registrar) { return (DlmanageUtils.trace(order, status).assignee.userId == registrar.id) };
  transition15.validate = function(status, order, now, registrar) { return (DlmanageUtils.trace(order, status).assignee.userId == registrar.id) };
  
  transition5.custom = function(status, order, now, registrar, model, callbackFunc, pos, host) { var div = document.createElement('div');
                                                                                                 MiscUtils.dialog(popupBox, pos, div, null, null);
                                                                                                 var func = function() { transition5.operate(status, order, now, registrar, model, callbackFunc, host);
                                                                                                                         popupBox._close();
                                                                                                                       };
                                                                                                 new DialogAssignee(div, DlmanageUtils.trace(order, status), func, { php: '{{$smarty.const.REMOTE|escape:javascript}}', condition: 'd.type = \'{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}\'', close: function() { popupBox._close(); } });
                                                                                               };
  
  model.states = [ state1, state2, state3, state4, state5, state6, state7, state8, state9, state10, state11, state12, state13, state14, state15, state16, state17, state18, state19, state20 ];
  model.transitions = [ transition1, transition2, transition3, transition4, transition5, transition6, transition7, transition8, transition9, transition10, transition11, transition12, transition13, transition14, transition15, transition16, transition17, transition18, transition19, transition20, transition21, transition22, transition23, transition24 ];
  return model;
};